package com.echo.activity;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.activeandroid.Model;
import com.echo.SportsApplication;
import com.echo.http.HttpCallBack;
import com.echo.http.HttpManager;
import com.echo.http.HttpUrl;
import com.echo.model.User;
import com.echo.serializer.UserSerializer;
import com.echo.util.ActiveAndroidStrategy;
import com.echo.util.DrawableUtil;
import com.echo.util.LogUtils;
import com.echo.util.PasswordUtil;
import com.echo.util.ToastUtils;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

import org.json.JSONException;
import org.json.JSONObject;

import butterknife.ButterKnife;
import butterknife.InjectView;

public class LoginActivity extends AppCompatActivity {


    @InjectView(R.id.toolbar_title)
    TextView toolbarTitle;
    @InjectView(R.id.toolbar)
    Toolbar toolbar;
    @InjectView(R.id.m_username)
    EditText mUsername;
    @InjectView(R.id.m_password)
    EditText mPassword;
    @InjectView(R.id.m_login_btn)
    Button mLoginBtn;
    @InjectView(R.id.m_to_regist_btn)
    TextView mToRegistBtn;
    @InjectView(R.id.login_layout)
    LinearLayout loginLayout;

    private int mType = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        ButterKnife.inject(this);
        initView();
        initActionBar();
    }

    private void initActionBar() {
        setSupportActionBar(toolbar);
        toolbar.setNavigationIcon(DrawableUtil.tintDrawable(getResources()
                .getDrawable(R.mipmap.ic_arrow_back_black_36dp), Color.parseColor("#ffffff")));
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    private void initView() {
        /*mUsername= (EditText) findViewById(R.id.login_username);
        mPassword= (EditText) findViewById(R.id.login_password);*/
        mToRegistBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mType == 0) {
                    toRegist();
                } else {
                    toLogin();
                }
            }
        });
    }

    private void toRegist() {
        mType = 1;
        mToRegistBtn.setText("登录");
        mLoginBtn.setText("注册");
        //.setText("注册");
        loginLayout.setBackgroundColor(Color.parseColor("#9d55b8"));
    }

    private void toLogin() {
        mType = 0;
        mToRegistBtn.setText("注册");
        mLoginBtn.setText("登录");
        //toolbarTitle.setText("登录");
        loginLayout.setBackgroundColor(Color.parseColor("#eb4f38"));
    }

    public void login(View view) {
        if (mType == 0) {
            String url = HttpUrl.URL_LOGIN + "?username=" + mUsername.getText().toString() + "&password=" + PasswordUtil.getBase64(mPassword.getText().toString());
            HttpManager.getInstance().login(url, new HttpCallBack() {
                @Override
                public void onMsg(Object response) throws JSONException {
                    JSONObject jsonObject = (JSONObject) response;
                    if (!jsonObject.has("message")) {
                        Gson gson = new Gson();
                        User user= (User) new UserSerializer().deserialize(jsonObject.toString());
                        SportsApplication.login(user);
                        Toast.makeText(LoginActivity.this, "登录成功", Toast.LENGTH_SHORT).show();
                        finish();
                    } else {
                        Toast.makeText(LoginActivity.this, jsonObject.getString("message"), Toast.LENGTH_SHORT).show();
                        ToastUtils.show(LoginActivity.this, jsonObject.getString("message"), Toast.LENGTH_SHORT);
                        LogUtils.e("login_error", jsonObject.getString("message"));
                    }

                }

                @Override
                public void onError() {

                }
            });
        } else {
            String username = mUsername.getText().toString();
            String password = mPassword.getText().toString();
            String url = HttpUrl.URL_REGIST + "?username=" + username + "&password=" + PasswordUtil.getBase64(password);
            HttpManager.getInstance().regist(url, new HttpCallBack() {
                @Override
                public void onMsg(Object response) throws JSONException {
                    JSONObject object = (JSONObject) response;
                    if (object.has("message")) {
                        ToastUtils.show(LoginActivity.this, object.getString("message"), Toast.LENGTH_SHORT);
                    } else {
                        String json = object.toString();
                        Gson gson = new Gson();
                        User user = gson.fromJson(json, User.class);
                        SportsApplication.login(user);
                        finish();
                    }
                }

                @Override
                public void onError() {
                    ToastUtils.show(LoginActivity.this, "注册失败", Toast.LENGTH_SHORT);
                }
            });
        }
    }
}
